"use strict";
import { ProjectOptions } from "@vue/cli-service";
const path = require("path");

function resolve(dir: string) {
  return path.join(__dirname, dir);
}
function getDateTime() {
  let d: Date, s: string;
  d = new Date();
  s = d.getFullYear().toString(); // 取年份
  s += "" + ("0" + (d.getMonth() + 1)).substr(-2); // 取月份
  s += "" + ("0" + d.getDate()).substr(-2); // 取日期
  s += "" + ("0" + d.getHours()).substr(-2); // 取小时
  s += "" + ("0" + d.getMinutes()).substr(-2); // 取分
  // s += ':' + ('0' + d.getSeconds()).substr(-2); // 取秒
  return s;
}

const proxyApi = {
  'dev': 'http://res.dev.linpeng99.com/',
  'test': 'http://res.test.linpeng99.com/'
}[process.env.VUE_APP_TITLE!] || 'http://res.test.linpeng99.com/'

process.env.VUE_APP_BUILD_TIME = getDateTime();
process.env.VUE_APP_VERSION = require("./package.json").version + "_" + getDateTime();
process.env.VUE_APP_BASE_DOMAIN = process.env.NODE_ENV === "production" ? "/" : proxyApi;

module.exports = {
  css: {
    extract: false,
  },
  publicPath: process.env.NODE_ENV === "production" ? "./" : "/",
  pages: {
    index: {
      // entry for the page
      entry: "examples/beforeMain.ts",
      // the source template
      template: "public/index.html",
      // output as dist/index.html
      filename: "index.html",
      // when using title option,
      // template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
      title: "linpengDiffCompare",
      // chunks to include on this page, by default includes
      // extracted common chunks and vendor chunks.
      // chunks: ['chunk-vendors', 'chunk-common', 'index'],
    },
  },
  devServer: {
    proxy: {
      "/api": {
        target: proxyApi,
        changeOrigin: true,
        secure: false,
      },
    },
  },
  configureWebpack: {
    name: 'linpengDiffCompare',
    resolve: {
      alias: {
        '@': resolve('')
      },
      extensions: ['.ts', '.tsx', '.js']
    },
    module: {
      rules: [
        {
          test: /\.tsx?$/,
          use: [
            {
              loader: 'babel-loader',
              options: {
                presets: ['vca-jsx', '@vue/cli-plugin-babel/preset', ['@babel/preset-typescript', { allowNamespaces: true }]]
              }
            }
          ]
        }
      ]
    }
  },
  chainWebpack: (config) => {
    config.resolve.alias
      .set("$root", resolve(""))
      .set("$public", resolve("public"))
      .set("$dist", resolve("dist"))
      .set("@assets", resolve("src/assets"));
      config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap((options: { compilerOptions: { preserveWhitespace: boolean; }; }) => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()

    config.module
      .rule("images")
      .use("url-loader")
      .tap(() => {
        return {
          limit: 4096,
          fallback: {
            loader: "file-loader",
            options: {
              name: `img/[name].${process.env.VUE_APP_VERSION}.[ext]`,
            },
          },
        };
      });
    // 为js添加版本号
    config.output.chunkFilename(`js/[name].${process.env.VUE_APP_VERSION}.js`);
    config.output.filename(`js/[name].${process.env.VUE_APP_VERSION}.js`);
  },
} as ProjectOptions;
